/*
 * 
 * OLDSKOOL DEMO
 * 
 * Java version. :)
 * 
 */
package oldskool1;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.DisplayMode;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;

/**
 *
 * @author menderleit
 */
public class Oldskool {

    private static final int SW = 800;
    private static final int SH = 600;
    private static final double POV = (double) SW / SH;
    // Demo globals...
    private static boolean demoRun = true;
    // Display stuff...
    private static JFrame frame;
    private static Window win;
    private static BufferStrategy bs;
    private static GraphicsDevice gd;
    private static GraphicsEnvironment env;
    private static DisplayMode[] dm;
    private static final DisplayMode[] DMODES = new DisplayMode[]{
        new DisplayMode(SW, SH, 32, 0),
        new DisplayMode(SW, SH, 16, 0)
    };
    private static DisplayMode dMode;
    // Key stuff...
    private static boolean SpacePressed = false;
    private static boolean SpaceLock = false;

    // Main entry point.
    public static void main(String[] args) {
        Random rnd = new Random();

        int numBalls = 488;
        Vectorballs vbCube = new Vectorballs(numBalls);

        int i = 0;
        for (int y = 0; y < 10; y++) {
            for (int x = 0; x < 10; x++) {
                for (int z = 0; z < 10; z++) {
                    if (y == 0 || y == 9 || x == 0 || x == 9 || z == 0 || z == 9) {
                        vbCube.posBall(i++, x - 5, y - 5, z - 5);
                    }
                }
            }
        }

        int loops = 30;
        Vectorballs vbTorus = new Vectorballs(loops * (loops / 2));

        i = 0;
        for (int r = 0; r < 360; r += 360 / loops) {
            for (int r2 = 0; r2 < 360; r2 += 360 / (loops / 2)) {
                double iX = 0.0;
                double iY = Math.sin(Math.toRadians(r2)) * 1.5 + 5.0;
                double iZ = Math.cos(Math.toRadians(r2)) * 1.5;
                double oX = iX;
                double oY = iY;
                iX = oX * Math.cos(Math.toRadians(r)) - oY * Math.sin(Math.toRadians(r));
                iY = oX * Math.sin(Math.toRadians(r)) + oY * Math.cos(Math.toRadians(r));
                vbTorus.posBall(i, iX, iY, iZ);
                i++;
            }
        }

        if (!initDemo()) {
            return;
        }

        long difTime = 0;
        long oldTime = System.currentTimeMillis();
        int frameCount = 0;
        long frameTime = 1000;
        int screenFPS = 0;

        double xAngle = 0.0;
        double yAngle = 0.0;

        win.toFront();
        while (demoRun) {
            difTime = System.currentTimeMillis() - oldTime;
            oldTime = System.currentTimeMillis();

            frameCount++;
            frameTime -= difTime;
            if (frameTime <= 0) {
                screenFPS = frameCount;
                frameCount = 0;
                frameTime += 1000;
            }

            xAngle += 0.045 * difTime;
            yAngle += 0.0666 * difTime;
            if (xAngle >= 360.0) {
                xAngle -= 360.0;
            }
            if (yAngle >= 360.0) {
                yAngle -= 360.0;
            }

            Graphics g = bs.getDrawGraphics();

            g.setColor(Color.black);
            g.fillRect(0, 0, SW, SH);
            vbCube.draw(g, SW, SH, POV, xAngle, xAngle);
            //vbTorus.draw(g, SW, SH, POV, xAngle, yAngle);

            g.setColor(Color.YELLOW);
            g.drawString("FPS: " + screenFPS, 0, 10);
            bs.show();
            g.dispose();
        }

        quitDemo();
    }

    // Finds a DisplayMode we can use.
    private static DisplayMode getBestDisplayMode() {
        for (int i = 0; i < dm.length; i++) {
            for (int j = 0; j < DMODES.length; j++) {
                if (dm[i].getWidth() == DMODES[j].getWidth() && dm[i].getHeight() == DMODES[j].getHeight() && dm[i].getBitDepth() == DMODES[j].getBitDepth()) {
                    return DMODES[j];
                }
            }
        }
        return null;
    }

    // Sets up the frame and window, and activates fullscreen exclusive mode.
    private static boolean initDemo() {
        frame = new JFrame();
        frame.setSize(SW, SH);
        frame.setLocation(0, 0);
        frame.setResizable(false);
        frame.setBackground(Color.black);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setIgnoreRepaint(true);
        frame.setVisible(true);

        // Make sure the window gets focus when it comes to the front.
        win = new Window(frame);
        win.setFocusable(true);
        win.setAutoRequestFocus(true);

        // Create an invisible cursor. (We don't want one)
        Toolkit tk = Toolkit.getDefaultToolkit();
        Image curs = tk.getImage("cursor.png");
        Point hotSpot = new Point(0, 0);
        Cursor cursor = tk.createCustomCursor(curs, hotSpot, "Invisible");
        win.setCursor(cursor);

        // Create a KeyListener so we can control the demo.
        win.addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent ke) {
                int key = ke.getKeyCode();
                switch (key) {
                    case KeyEvent.VK_ESCAPE:
                        demoRun = false;
                        break;
                    case KeyEvent.VK_SPACE:
                        SpacePressed = true;
                        SpaceLock = true;
                        break;
                }
            }

            @Override
            public void keyReleased(KeyEvent ke) {
                int key = ke.getKeyCode();
                switch (key) {
                    case KeyEvent.VK_SPACE:
                        SpacePressed = false;
                        SpaceLock = false;
                        break;
                }
            }
        });

        // Create the GraphicsDevice and get a useable DisplayMode.
        env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        gd = env.getDefaultScreenDevice();
        dm = gd.getDisplayModes();
        dMode = getBestDisplayMode();
        if (dMode == null) {
            quitDemo();
            return false;
        }

        // Set fullscreen exclusive mode, and use our own displaymode.
        gd.setFullScreenWindow(win);
        gd.setDisplayMode(dMode);

        // Create a bufferstrategy for the window with double-buffering.
        win.createBufferStrategy(2);
        bs = win.getBufferStrategy();
        return true;
    }

    // Closes down the application in an orderly fashion. :)
    public static void quitDemo() {
        gd.setFullScreenWindow(null);
        if (win != null) {
            win.dispose();
        }
        if (frame != null) {
            frame.dispose();
        }
    }
}
